package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.DynamicListContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.DynameicListPresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.DynameicAdapter;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/19 15:06
 * 功能描述： 她的动态
 * 联系方式：1037438704@qq.com
 */
public class DynameicActivity extends BaseActivity implements DynamicListContract, RecyclerArrayAdapter.OnItemClickListener {
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.frame_gone)
    FrameLayout frame_gone;
    private DynameicAdapter dynameicAdapter;
    private int count;
    private String member_id;
    private DynameicListPresenter dynameicListPresenter;
    private String jump;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_dynameic;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        dynameicListPresenter.getDynamicList(token, member_id);
    }

    private void initView() {
        dynameicListPresenter = new DynameicListPresenter(me, this);
        count = getIntent().getExtras().getInt("count");
        member_id = getIntent().getExtras().getString("member_id");
        jump = getIntent().getExtras().getString("jump");
        if (TextUtils.isEmpty(member_id)) {
            ToastUtil.show("数据错误");
            return;
        }
        if (count == 1) {
            if (getSex.equals("1")) {
                textCenter.setText("她的动态");
            } else {
                textCenter.setText("他的动态");
            }

        } else {
            textCenter.setText("我的动态");
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dynameicAdapter = new DynameicAdapter(this, count);
        recyclerView.setAdapter(dynameicAdapter);
        dynameicAdapter.setOnItemClickListener(this);
        dynameicAdapter.setmOnClick(new DynameicAdapter.ItemOnClick() {
            @Override
            public void onClick(int position, Map<String, String> data, int type) {
                if (type == 1) {
                    dynameicListPresenter.addOrderGood(token, data.get("id"), position);
                }
            }
        });
    }

    private void initData() {
        imageleftFinish.setVisibility(View.VISIBLE);

    }

    @OnClick({R.id.image_left_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            default:
        }
    }


    public static void start(Context me, int count, String member_id, String jump) {
        Bundle bundle = new Bundle();
        Intent intent = new Intent(me, DynameicActivity.class);
        bundle.putInt("count", count);
        bundle.putString("member_id", member_id);
        bundle.putString("jump", jump);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }

    @Override
    public void dycSuccess(List<Map<String, String>> data) {
        if (data == null || data.size() == 0) {
            frame_gone.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            frame_gone.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
        dynameicAdapter.clear();
        dynameicAdapter.addAll(data);
    }

    @Override
    public void dycFailed(String msg) {

    }

    @Override
    public void showLoading() {
        WaitDialog.show(me, "");
    }

    @Override
    public void addOrderGood(int poation) {
        List<Map<String, String>> allData = dynameicAdapter.getAllData();
        String good_count = allData.get(poation).get("good_count");
        allData.get(poation).put("good_count", (Integer.valueOf(good_count) + 1) + "");
        dynameicAdapter.notifyItemChanged(poation);
    }

    @Override
    public void onItemClick(int position) {
        if (jump.equals("1")) {
            return;
        }
        List<Map<String, String>> allData = dynameicAdapter.getAllData();
//        ToastUtil.show(allData.get(position).get("type"));
        DateDetailsActivity.start(me, count + "", allData.get(position).get("id"), "1");
    }
}
